package com.genesis.web.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.common.Result;
import com.genesis.mall.entity.PmsBrand;
import com.genesis.mall.service.IPmsBrandService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.genesis.mall.common.PageInfo;
import com.genesis.mall.common.Result;
import com.genesis.mall.entity.PmsBrand;
import com.genesis.mall.service.IPmsBrandService;
import com.genesis.mall.vo.UpdateFactoryStatusVo;
import com.genesis.mall.vo.UpdateShowStatusVo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
//TODO 前后台冲突
@Slf4j
@RestController
@RequestMapping("/brand")
public class PmsBrandController {

    @Autowired
    private IPmsBrandService iPmsBrandService;

    /**
     * 分页获取推荐品牌
     * @param pageNum
     * @param pageSize
     * @return
     */
      @GetMapping("recommendList")
      public Result recommendList(int pageNum, int pageSize){
     //1.构建分页构造器
          Page<PmsBrand> pmsBrandPage = new Page<>(pageNum, pageSize);

          LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();

         Page<PmsBrand> page = iPmsBrandService.page( pmsBrandPage,queryWrapper);

          List<HashMap> hashMaps = page.getRecords().stream().map(item -> {
              HashMap hashMap = JSON.parseObject(JSON.toJSONString(item), HashMap.class);
              return hashMap;
          }).collect(Collectors.toList());

          return Result.success(hashMaps);

      }
/**
 * 获取品牌详情
 */
        //2.查看单个列表
     @GetMapping("/detail/{id}")
    public Result<PmsBrand> detail( @PathVariable Long id){

         PmsBrand pmsBrand = iPmsBrandService.getById(id);

         return  Result.success(pmsBrand,"操作成功");
     }




    /**
     * 分页获取品牌相关商品
     * @param id
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/productList")
    public Result productList (Long id,int pageNum,int pageSize){
        //1.分页获取数据
        Page<PmsBrand> page = new Page<>(pageNum, pageSize);
        //2.
        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsBrand::getId,id);
        Page<PmsBrand> pmsBrandPage = iPmsBrandService.page(page, queryWrapper);

        ArrayList<PmsBrand> list = new ArrayList<>();
        List<PmsBrand> records = pmsBrandPage.getRecords();
        for (PmsBrand record : records) {
            PmsBrand pmsBrand = new PmsBrand();
            BeanUtils.copyProperties(record,pmsBrand);
            list.add(pmsBrand);

        }


        int total = (int)pmsBrandPage.getTotal();
        int totalPage = (int)pmsBrandPage.getPages();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("totalPage",totalPage);
        map.put("total",total);
        map.put("list",list);

        return  Result.success(map,"操作成功");

    }
    @Autowired
    private IPmsBrandService  pmsBrandService;

    /**
     * 增加品牌
     *
     * @param pmsBrand
     * @return
     */
    @PostMapping("/create")
    public Result addBrand(@RequestBody PmsBrand pmsBrand) {
        pmsBrandService.save(pmsBrand);
        return Result.success("add");
    }

    /**
     * 批量删除品牌
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete/batch")
    public Result delete(@RequestBody List<Long> ids) {
        pmsBrandService.removeByIds(ids);
        return Result.success("del");
    }

    /**
     * 根据id删除品牌
     *
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public Result deleteId(@PathVariable("id") Long id) {
        pmsBrandService.removeById(id);
        return Result.success("deleteById");
    }

    /**
     * 根据品牌列表名称分页获取品牌列表
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public Result List(String keyword, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {
        PageInfo brandList = pmsBrandService.listBrand(keyword, pageNum, pageSize);
        return Result.success(brandList);

    }
    /**
     * 获取全部品牌列表
     *
     * @return
     */
    @GetMapping("/listAll")
    public Result findAll() {
        List<PmsBrand> list = pmsBrandService.list();
        return Result.success(list);
    }

    /**
     * 批量更新厂家制造商状态
     * @param updateFactoryStatusVo
     * @return
     */
    @PostMapping("update/factoryStatus")
    public Result  UpdateFactoryStatus( UpdateFactoryStatusVo updateFactoryStatusVo){
        pmsBrandService.updateFactoryStatus(updateFactoryStatusVo.getIds(),updateFactoryStatusVo.getFactoryStatus());
        return Result.success();
    }

    /**
     * 批量更新显示状态
     * @param updateShowStatusVo
     * @return
     */
    @PostMapping("update/showStatus")
    public Result updateShowStatus(  UpdateShowStatusVo updateShowStatusVo) {
        pmsBrandService.updateShowStatus(updateShowStatusVo.getIds(),updateShowStatusVo.getShowStatus());
        return Result.success();
    }

    /**
     * 修改品牌
     * @param id
     * @param
     * @return
     */
    @PostMapping("/update/{id}")
    public Result update(@PathVariable("id") Long id,
                         @RequestBody  PmsBrand pmsBrand) {
        //pmsBrand.setId(id);
        log.info(pmsBrand.toString());
        pmsBrandService.updateById(pmsBrand);
        return Result.success();
    }

    /**
     * 根据编号查品牌
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getId(@PathVariable  Long id) {
        PmsBrand pmsBrand=pmsBrandService.getById(id);
        return Result.success(pmsBrand);
    }


}
